//
//  BasePosterView.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/25.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "BasePosterView.h"

@implementation BasePosterView

#pragma mark -init function
- (nonnull instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(nonnull UICollectionViewLayout *)layout {
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}

#pragma mark -collection view's delegate and datasourse function

//cell的尺寸
- (CGSize)collectionView:(nonnull UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return CGSizeMake(self.itemWidth, self.bounds.size.height);
}

- (UIEdgeInsets)collectionView:(nonnull UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    //设置边缘距离使第一张图片显示在屏幕中心
    return UIEdgeInsetsMake(0, (kWidth - self.itemWidth) / 2, 0, (kWidth - self.itemWidth) / 2);
}

//停止滚动的时候停在cell的中心线上
- (void)scrollViewWillEndDragging:(nonnull UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout nonnull CGPoint *)targetContentOffset {
    CGFloat x = targetContentOffset -> x - self.itemWidth / 2;
    CGFloat linespacing = lineSpacing;
    int index = x / (self.itemWidth + linespacing);
    targetContentOffset -> x = index * (self.itemWidth + linespacing) ;
    //设定当前所在cell
    self.currentIndex = index;
}

- (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return nil;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageData.count;
}

#pragma mark  -setter getter
- (void)setImageData:(NSMutableArray *)imageData
{
    _imageData = imageData;
    [self reloadData];
}



@end
